package com.hank.jms.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Enumeration;

public class Sender3 {
    public static String BROKER_URL = "tcp://10.10.1.44:61616";

    public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
        Connection connection = null;
        try {
            //通过工厂获取一个连接
//            connection = connectionFactory.createConnection();
//            connection.start();
//
//            Enumeration enumeration = connection.getMetaData().getJMSXPropertyNames();
//            while (enumeration.hasMoreElements()) {
//                System.out.println(enumeration.nextElement().toString());
//            }
            createSession(connectionFactory);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void createSession(ConnectionFactory connectionFactory) throws JMSException {
        //通过工厂获取一个连接
        Connection  connection = connectionFactory.createConnection();
        connection.start();
        for (int i = 0; i < 30; i++) {
            //通过连接创建一个会话,并默认设开启事务,自动确认消息
            final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            //通过会话构建一个目的地
            Destination destination = session.createQueue("static-network-queue");
            //再通过session创建一个消息发送者，并设定发送目的地对象
            final MessageProducer messageProducer = session.createProducer(destination);

            Thread thmsg = new Thread(new SendMessage(String.valueOf(i), messageProducer, session));
            thmsg.start();
        }

    }
}

class SendMessage implements Runnable {

    String name = "";
    MessageProducer messageProducer;
    Session session;
    long num = 0;

    public SendMessage(String name, MessageProducer messageProducer, Session session) {
        this.name = "SessionID:" + name;
        this.messageProducer = messageProducer;
        this.session = session;
    }

    public void run() {
        while (true) {
            try {
                num++;
                sendMessage(this.name, this.messageProducer, this.session);
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void sendMessage(String name, MessageProducer messageProducer, Session session) throws JMSException {
        for (int i = 0; i < 100; i++) {
            TextMessage message = session.createTextMessage("This is TextMessage " + name + ">>" + i+"--"+System.currentTimeMillis()+this.getClass().toString());
            messageProducer.send(message);
        }

        session.commit();
        System.out.println(name + " Send message " + num);

    }
}